import time

from selenium.webdriver.common.by import By

from common.Utils import BlogDriver
#测试博客编辑页面


class BlogEdit:
    url = ""
    driver = ""
    def __init__(self):

        try:
            alert = BlogDriver.driver.switch_to.alert
            alert.accept()  # 点击确认关闭alert
        except:
            pass  # 如果没有alert就继续




        self.url = "http://8.137.19.140:9090/blog_edit.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    #测试编辑页面（登录状态下）
    def EditSucTestByLogin(self):
        #检查页面是否存在
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)").click()
        time.sleep(1)
        #检查输入博客标题
        self.driver.find_element(By.CSS_SELECTOR,"#title").send_keys("新博客")
        #点击提交
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        #查看博客是否提交成功
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.right > div:nth-child(6) > div.title")


        #注销登录
        self.driver.find_element(By.CSS_SELECTOR, "body > div.nav > a:nth-child(6)").click()




    def EditFailTestUnLogin(self):

        self.driver.find_element(By.CSS_SELECTOR, "#title").send_keys("未登录状态")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()

        time.sleep(1)
        # 未登录情况无法提交，返回登录页面
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container-login > div > h3")